package logic

import (
	"context"
	"errors"
	"strings"
	"video/models/mysql"
	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论添加
func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户未注册,不允许添加评论")
	}

	comment := &mysql.Comment{
		Model:   gorm.Model{},
		VideoId: int(in.VideoId),
		UserId:  int(in.UserId),
		Content: in.Content,
	}
	if in.Content == "" {
		return nil, errors.New("请输入评论内容")
	}
	if strings.Contains(in.Content, "垃圾") || strings.Contains(in.Content, "骗人") {
		return nil, errors.New("含违规词汇，请修改")
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("评论失败")
	}

	return &videorpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
